//
//  LSYButton.m
//  TTClub
//
//  Created by xkun on 15/8/28.
//  Copyright (c) 2015年 TTClub. All rights reserved.
//
#import "LSYButton.h"

@interface LSYButton ()


@property (nonatomic) BOOL ignoreEvent;

@end

@implementation LSYButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    [self setExclusiveTouch:NO];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    switch (_lsyButtonStyle) {
        case LSYButtonStyleImageTitleNomarl: {
            break;
        }
        case LSYButtonStyleImageTitleVertical: {
//            if (self.lsy_isFitTextWidth) {
//                CGSize size = [PublicMethod sizeWithUILabel:self.titleLabel
//                                             andContainSize:(CGSize){self.width*1.5, self.height}];
//                
//                [self.titleLabel setWidth_:ceilf(size.width)];
//            }
            [self.imageView setCenterX_:ceilf(self.width/2)];
            [self.titleLabel setCenterX_:self.imageView.centerX];
            break;
        }
        default: {
            break;
        }
    }
}

//解决连续点击问题
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    
#ifndef TT_USE_BUGTAGS
    if (!self.ignoreEvent) {
        self.ignoreEvent = YES;
        [super sendAction:action to:target forEvent:event];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.ignoreEvent = NO;
    });
#else
    [super sendAction:action to:target forEvent:event];
#endif
    
}

#pragma mark - Getter

#pragma mark - Methods


- (void)setUpImageAndDownTitleByMargin:(CGFloat)margin
{
    CGPoint buttonBoundsCenter    = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGPoint endImageViewCenter    = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(self.imageView.bounds));
    CGPoint endTitleLabelCenter   = CGPointMake(buttonBoundsCenter.x,
                                                CGRectGetHeight(self.bounds)-
                                                CGRectGetMidY(self.titleLabel.bounds));
    
    CGPoint startImageViewCenter  = self.imageView.center;
    CGPoint startTitleLabelCenter = self.titleLabel.center;
    CGFloat imageEdgeInsetsTop    = -margin;
    CGFloat imageEdgeInsetsLeft   = endImageViewCenter.x - startImageViewCenter.x;
    CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;
    CGFloat imageEdgeInsetsRight  = -imageEdgeInsetsLeft;
    
    self.imageEdgeInsets        = UIEdgeInsetsMake(imageEdgeInsetsTop,
                                                     imageEdgeInsetsLeft,
                                                     imageEdgeInsetsBottom,
                                                     imageEdgeInsetsRight);
    
    CGFloat titleEdgeInsetsTop    = ceilf(fabs(margin)*2)+2;
    CGFloat titleEdgeInsetsLeft   = endTitleLabelCenter.x - startTitleLabelCenter.x;
    CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop;
    CGFloat titleEdgeInsetsRight  = -titleEdgeInsetsLeft;
    
    self.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop,
                                              titleEdgeInsetsLeft,
                                              titleEdgeInsetsBottom,
                                              titleEdgeInsetsRight);
}

- (void)setButtonTitleAndImageByMargin:(CGFloat)margin
                              isNormal:(BOOL)isNormal
{
    if (!isNormal) {
//        [self setTitleEdgeInsets:UIEdgeInsetsMake(0,-self.imageView.image.size.width, 0, 0)];
//        [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0,0)];
//        
//        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.image.size.width, 0, self.imageView.image.size.width)];
//        [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0, -self.titleLabel.bounds.size.width)];
        
//        CGFloat spacing = 10; // the amount of spacing to appear between image and title
//        UIEdgeInsets imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0, spacing  -self.titleLabel.bounds.size.width);
//        UIEdgeInsets titleEdgeInsets = UIEdgeInsetsMake(0, spacing - self.imageView.image.size.width * 2, 0, self.imageView.image.size.width);
//        
//        self.imageEdgeInsets = imageEdgeInsets;
//        self.titleEdgeInsets = titleEdgeInsets;
    }
    
//    UIEdgeInsets imageEdge = self.imageEdgeInsets;
//    UIEdgeInsets titleEdge = self.titleEdgeInsets;
//    
//    if (isNormal) {
//        self.imageEdgeInsets = imageEdge;
//        self.titleEdgeInsets = titleEdge;
//    } else {
//        imageEdge.right = -CGRectGetWidth(self.bounds);
//        titleEdge.left = margin/2;
//        self.imageEdgeInsets = imageEdge;
//        self.titleEdgeInsets = titleEdge;
//    }
}

@end
